package org.jgs1904.work.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.jgs1904.util.DataSourceUtil;
import org.jgs1904.work.entity.Message;

/**
 * 留言数据库操作类
 * @author dyf
 * @date 2020年2月4日
 */
public class MessageDao {

	public List<Message> selectAllByTargetId(Long id) throws SQLException {

		QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
		
		String sql = "select message.*,users.real_name as 'author_name' from message,users where author_id = users.id and target_id = ? order by create_time desc";
		
		List<Message> result = qr.query(sql, new BeanListHandler<>(Message.class), id);
		
		return result;
	}

	public int insert(Message message) throws SQLException {

		QueryRunner qr = new QueryRunner(DataSourceUtil.getDataSource());
		
		String sql = "insert into message(author_id,target_id,content,create_time) value(?,?,?,?)";
		
		int result = qr.update(sql, message.getAuthor_id(), message.getTarget_id(), message.getContent(), message.getCreate_time());
		
		return result;
	}

}
